package com.neusoft.restaurant.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.restaurant.common.exception.BizException;
import com.neusoft.restaurant.config.utils.PageUtils;
import com.neusoft.restaurant.entity.PageResult;
import com.neusoft.restaurant.entity.ResAddress;
import com.neusoft.restaurant.entity.ResOrders;
import com.neusoft.restaurant.entity.ResOrdersDetail;
import com.neusoft.restaurant.mapper.ResAddressMapper;
import com.neusoft.restaurant.mapper.ResOrdersDetailMapper;
import com.neusoft.restaurant.mapper.ResOrdersMapper;
import com.neusoft.restaurant.service.IResOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 订单Service业务层处理
 * 
 * @author zhongda
 * @date 2023-12-10
 */
@Service
public class ResOrdersServiceImpl implements IResOrdersService
{
    @Autowired
    private ResOrdersMapper resOrdersMapper;

    @Autowired
    private ResOrdersDetailMapper resOrdersDetailMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private ResAddressMapper addressMapper;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public ResOrders selectResOrdersById(Long id)
    {
        ResOrders resOrders=resOrdersMapper.selectResOrdersById(id);
        ResOrdersDetail detail=new ResOrdersDetail();
        detail.setOrderId(id);
       List<ResOrdersDetail>  list=resOrdersDetailMapper.selectResOrdersDetailList(detail);
       resOrders.setOrdersDetails(list);

        return resOrders;
    }

    /**
     * 查询订单列表
     * 
     * @param resOrders 订单
     * @return 订单
     */
    @Override
    public List<ResOrders> selectResOrdersList(ResOrders resOrders)
    {
        return resOrdersMapper.selectResOrdersList(resOrders);
    }

    @Override
    public PageResult selectResOrdersList2(ResOrders resOrders, Integer pageNum, Integer pageSize) {
        //1、PageHelper开始分页，传入第几页和每页多少条记录两个参数
        PageHelper.startPage(pageNum, pageSize);
        //2、数据库进行查询
        List<ResOrders> resOrderss = resOrdersMapper.selectResOrdersList(resOrders);
        //3、对返回值进行判断
        if (resOrderss.size()==0){
            return null;
        }
        //4、对查询的结果进行封装
        PageInfo<ResOrders> info = new PageInfo<>(resOrderss);
        PageResult pageResult = PageUtils.getPageResult(info);

        return pageResult;
    }

    /**
     * 新增订单
     * 
     * @param resOrders 订单
     * @return 结果
     */
    @Override
    public long insertResOrders(ResOrders resOrders)
    {

        //获得当前用户的id
        Long userId= (Long) request.getAttribute("userId");



//        //查询用户数据
//        User user = userService.getById(userId);
        //查询地址数据
        Long addressBookId = resOrders.getAddressBookId();
        ResAddress addressBook = addressMapper.selectResAddressById(addressBookId);

        if (addressBook == null) {
            throw new BizException("地址信息有误，不能下单");
        }
        Long orderId=System.currentTimeMillis();
        resOrders.setNumber(orderId+"");
        resOrders.setStatus(1L);
        resOrders.setUserId(userId);
        resOrders.setOrderTime(new Date());
        List<ResOrdersDetail> list=resOrders.getOrdersDetails();


            int res= resOrdersMapper.insertResOrders(resOrders);

            if (res>0){
                if (list.size()>0){
                    for (ResOrdersDetail detail:list) {
                        detail.setOrderId(resOrders.getId());
                    int resDetail= resOrdersDetailMapper.insertResOrdersDetail(detail);

                    }
                }else {
                    throw new BizException("创建订单失败");
                }

            }

        return resOrders.getId();







    }

    /**
     * 修改订单
     * 
     * @param resOrders 订单
     * @return 结果
     */
    @Override
    public int updateResOrders(ResOrders resOrders)
    {
        return resOrdersMapper.updateResOrders(resOrders);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteResOrdersByIds(Long[] ids)
    {
        return resOrdersMapper.deleteResOrdersByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteResOrdersById(Long id)
    {
        return resOrdersMapper.deleteResOrdersById(id);
    }
}
